package marchsoft.modules.tiku.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;
import marchsoft.base.PageVO;
import marchsoft.modules.tiku.entity.ESafeCase;

import marchsoft.modules.tiku.entity.dto.ESafeCaseNameDto;
import marchsoft.modules.tiku.entity.dto.ESafeCaseSearchDto;
import marchsoft.modules.tiku.mapper.ESafeCaseMapper;
import marchsoft.modules.tiku.service.IESafeCaseService;
import marchsoft.base.BasicServiceImpl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import marchsoft.utils.FileUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* <p>
* 安全案例 服务实现类
* </p>
*
* @author zsy
* @since 2022-03-29
*/
@Service
@RequiredArgsConstructor
public class ESafeCaseServiceImpl extends BasicServiceImpl<ESafeCaseMapper, ESafeCase> implements IESafeCaseService {

    private final ESafeCaseMapper eSafeCaseMapper;

    @Override
    public IPage<ESafeCaseNameDto> queryAllCase(PageVO pageVO, ESafeCaseSearchDto searchDto) {
        QueryWrapper<Object> queryWrapper = new QueryWrapper<>();

        if (ObjectUtil.isNotNull(searchDto.getKeywords())) {
            queryWrapper.and(e -> {
                e.like("sc.title",searchDto.getKeywords()).or().like("sc.keywords",searchDto.getKeywords());
            });
        }

        if (ObjectUtil.isNotNull(searchDto.getSafeTypeId())) {
            queryWrapper.eq("sc.safe_type",searchDto.getSafeTypeId());
        }
        queryWrapper.eq("sc.is_deleted",0);
        pageVO.setOrders("[{'column': 'sc.create_time','asc': true}]");
        IPage<ESafeCase> page = pageVO.buildPage();
        IPage<ESafeCaseNameDto> eSafeCases = eSafeCaseMapper.queryAllCase(page,queryWrapper);
        return eSafeCases;
    }

    @Override
    public ESafeCase queryPidCase(Long id) {
        ESafeCase eSafeCases = eSafeCaseMapper.queryPidCase(id);
        return eSafeCases;
    }

    @Override
    public void deleteById(Long id) {
        eSafeCaseMapper.deleteById(id);
    }

    @Override
    public void updateESafeCase(ESafeCase eSafeCase) {
        eSafeCaseMapper.updateESafeCase(eSafeCase);
    }

    @Override
    public void addESafeCase(ESafeCase eSafeCase) {
        eSafeCaseMapper.addESafeCase(eSafeCase);
    }

    @Override
    public void download(IPage<ESafeCaseNameDto> queryAllCase, HttpServletResponse response) throws IOException {
        List<ESafeCaseNameDto> records = queryAllCase.getRecords();

        List<Map<String, Object>> list = new ArrayList<>();
        for (ESafeCaseNameDto record : records) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("标题", record.getTitle());
            map.put("关键字", record.getKeywords());
            if (ObjectUtil.isNull(record.getSafeName())) {
                map.put("类型", "其他类型");
            }else {
                map.put("类型", record.getSafeName());
            }
            map.put("点赞数",record.getTags());
            list.add(map);
        }
        FileUtils.downloadExcel(list, response);
    }
}

